package com.xuecheng.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.auth.mapper.XcTeacherMapper;
import com.xuecheng.auth.model.po.XcTeacher;
import com.xuecheng.auth.service.XcTeacherService;
import com.xuecheng.base.exception.XueChengPlusException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class XcTeacherServiceImpl extends ServiceImpl<XcTeacherMapper, XcTeacher>
        implements XcTeacherService {
    @Override
    public XcTeacher saveTeacher(XcTeacher teacher) {
        if(teacher == null){
            XueChengPlusException.cast("教师信息不能为空");
        }
        if(teacher.getId() == null){
            save(teacher);
        }else{
            updateById(teacher);
        }
        return getById(teacher.getId());
    }

    @Override
    public XcTeacher getTeacher(XcTeacher teacher) {
        if(teacher == null){
            return null;
        }
        if(teacher.getId() != null){
            return getById(teacher.getId());
        }
        Long userId = teacher.getUserId();
        String name = teacher.getName();
        String number = teacher.getNumber();
        String intro = teacher.getIntro();
        String resume = teacher.getResume();
        String pic = teacher.getPic();
        return getOne(new LambdaQueryWrapper<XcTeacher>()
                .eq(userId != null, XcTeacher::getUserId, userId)
                .eq(StringUtils.isNotBlank(name), XcTeacher::getName, name)
                .eq(StringUtils.isNotBlank(number), XcTeacher::getNumber, number)
                .eq(StringUtils.isNotBlank(intro), XcTeacher::getIntro, intro)
                .eq(StringUtils.isNotBlank(resume), XcTeacher::getResume, resume)
                .eq(StringUtils.isNotBlank(pic), XcTeacher::getPic, pic));
    }
}
